#ifndef __SHAREDMEMORY_H__
#define __SHAREDMEMORY_H__

#define SM_OVERHEAD (26)

typedef struct SM_t SM;

/*  Create shared memory at the size of _size, and naming it: _memName.                     *
 *  The size must be size + SM_OVERHEAD for manage the shared memory block.                 *
 *  The name is path has to have only characters without slashes after, and                 *
 *  has at least 1 character after.                                                         *
 *  example: if want 1000 bytes of memory.                                                  *
 *  SM* smem = SMCreate(1000 + SM_OVERHEAD, "shmem1")                                       *
 *  Return value: On success return SM handle, fail return 0.                              */
SM* SMCreate(size_t _size, const char* _memName);

/*  Attach to existing shared memory by name.                                               *
 *  On success return SM handle, fail return 0.                                            */
SM* SMAttach(const char* _memName);

/*  Lock shared memory to the current process. Every process trying lock it will wait until *
 *  the curr process unlock it. On success return 0 , fail error numbers                   */
int SMLock(SM* );

/*  Lock shared memory. If it locked by other process the trying process will wait the      *
 *  amount of seconds given in _seconds, than go on without locking.                        *
 *  On success return 0 , fail error numbers.                                              */
int SMLockTimeOut(SM* _sm, long _seconds);

int SMUnlock(SM* );

/*  Get the shared memory buffer for read an write. Remember to lock the SM before writing  *
 *  to it. On success return pointer in virtual memory for the shared memory, on fail 0.   */
void* SMGet(SM* );

/*  Deatach from the shared memory. Delete the SM handler. If it the last process that      *
 *  deatach remove the shared memory.                                                      */
void SMDetach(SM*);

#endif /* __SHAREDMEMORY_H__    */
